package com.leetao.wxmsg.framework.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.leetao.wxmsg.common.utils.HttpUtils;
import com.leetao.wxmsg.framework.domain.WxMsg;
import com.leetao.wxmsg.framework.manage.WxMsgManager;
import com.leetao.wxmsg.framework.service.WxMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 菅江晖
 * @date 2021/3/8 - 14:16
 */
@Service
public class WxMsgServiceImpl implements WxMsgService {

    private final WxMsgManager wxMsgManager;

    private final static String[] SEND_API = {"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={}","POST"};

    @Autowired
    public WxMsgServiceImpl(WxMsgManager wxMsgManager) {
        this.wxMsgManager = wxMsgManager;
    }

    @Override
    public String sendMsg(WxMsg wxMsg) {
        return HttpUtils.httpRequest(CharSequenceUtil.format(SEND_API[0],wxMsgManager.getAccessToken()), SEND_API[1], JSON.toJSONString(wxMsg), WxMsgManager.JSON_TAG);
    }
}
